﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using System.IO;
using System.Text.RegularExpressions;
namespace MDS
{
    public class RepositoryKeyGenerator
    {
        private static Regex invalidFileNameCharsRegex;

        static RepositoryKeyGenerator()
        {
            char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
            string str = new string(invalidFileNameChars);
            str = Regex.Escape(str);
            invalidFileNameCharsRegex = new Regex(string.Format("[{0}]", str));
        }
        public static string GenerateKey(string name, DateTime tradeDay)
        {
            Contract.Requires(!string.IsNullOrEmpty(name));
            name = replaceInvalidPathChars(name, "_");
            return string.Format("{0}{1:yyyy:MM:dd}", name, tradeDay);
        }

        public static string replaceInvalidPathChars(string filename, string replaceChar)
        {
            return invalidFileNameCharsRegex.Replace(filename, replaceChar);
        }
    }
}
